package com.yy.service.impl;

import com.yy.bean.Employee;
import com.yy.bean.EmployeeExample;
import com.yy.dao.EmployeeMapper;
import com.yy.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeServiceImpl  implements EmployeeService {

    //EmployeeMapper 是一个接口 ,实例来自容器
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getAllEmps() {
        //System.out.println("=====service=========");
        List<Employee> list = employeeMapper.selectByExample(null);
        list.forEach(System.out::println);
        return list;
    }

    //根据条件查询
    public List<Employee> getEmpsByEmail(String email) {
        //employeeExample 封装查询条件
        EmployeeExample employeeExample =new EmployeeExample();
        //迭代器  封装条件
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        //and email="asda@QQ.com"
        criteria.andEmailEqualTo(email);
        List<Employee> list = employeeMapper.selectByExample(employeeExample);
        list.forEach(System.out::println);

        return null;
    }

    @Override
    public int deleteEmpById(Integer empId) {
       return employeeMapper.deleteByPrimaryKey(empId);
    }

    @Override
    public int updateEmpById(Employee employee) {

        return employeeMapper.updateByPrimaryKey(employee);
    }

    @Override
    public List<Employee> getEmpsByName(String empName) {
        EmployeeExample example =new EmployeeExample();
        EmployeeExample.Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        return employeeMapper.selectByExample(example);
    }

}
